package edu.ucsd.ccdb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FastCheckDirectoryServlet extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private ServletConfig config = null;
	private String codeBase = null;
	
	public void init (ServletConfig config) {
		
		try {
			super.init(config);
		} catch (ServletException e) {
			e.printStackTrace();
		}
		
		this.config = config;
	}
	
	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			
			
			this.codeBase = config.getServletContext().getInitParameter("codebase");
			if (request.getParameter("session") != null ) {
			}
			
			// 	get printer
			PrintWriter out = response.getWriter();
			
			response.setContentType("text/plain");
	        response.setHeader("Expires", "0"); //To avoid the caching of the JNLP file
	        response.addDateHeader("Date", java.util.Calendar.getInstance().getTime().getTime());
	        response.addDateHeader("Last-Modified", java.util.Calendar.getInstance().getTime().getTime());
	        out.println( getOutputFromRunCommand() );
	        out.close();
	        
		}
		catch (Exception e ){
			e.printStackTrace();
		}
	}
	
	public void doGet (HttpServletRequest request, HttpServletResponse response ) {
		try {
			processRequest(request, response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
													throws ServletException, IOException {
	      processRequest(request, response);
	}
	
	private String getOutputFromRunCommand () throws Exception {
		
		StringBuffer result = new StringBuffer();
		
		RunCheckRuntimeCommand.runCommand( "sh /home/thanhluan001/startIRODS" , result );
		return new String (result) ;
	}
	   
	private String generateJNLPContent(String session) {
		
		StringBuffer buff = new StringBuffer();
		
	    buff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
	    buff.append("<jnlp codebase=\""+this.codeBase+"\">\n");
	    buff.append("<information>\n");
	    buff.append("<title>Cell Centered Database (CCDB)</title>\n");
	    buff.append("<vendor>NCMIR - BIRN-CC</vendor>");
	    buff.append("<description>Application to upload data to the Cell CentereFasd Database</description>\n");
	    buff.append("</information>\n");
	    buff.append("<security>\n");
	    buff.append("<all-permissions/>\n");
	    buff.append("</security>\n");
	    buff.append("<resources>");
	    buff.append("<j2se version=\"1.5+\"/>\n");
	    buff.append("<jar href=\"CCDBUploadTool.jar\" main=\"true\" download=\"eager\"/>\n");
	    buff.append("</resources>\n");
	    buff.append("<application-desc main-class=\"edu.ccdb.Uploader\">\n");
	    buff.append("<argument>"+session+"</argument>\n");
	    buff.append("</application-desc>\n");
	    buff.append("</jnlp>\n");

	    return buff.toString();
	    
	}//End of invokeUploader()    
}
